package com.weayer.config;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @description: Redisson配置
 * @author: Weayer
 **/
@Component
public class RedisConfig {
    private String address = "redis://";

    @Value("${spring.redis.host}")
    private String redisHost;

    @Value("${spring.redis.port}")
    private String redisPort;

    // 基于springboot注解
    @Bean("redissonBootClient")
    public RedissonClient redissonBootClient(){
        Config config = new Config();
        config.useSingleServer().setAddress(address + redisHost +":"+ redisPort).setDatabase(0);
        return Redisson.create(config);
    }

    @Bean("redissonYamlClient")
    public RedissonClient redissonYamlClient(){
        Config config = null;
        try {
            config = Config.fromYAML(new ClassPathResource("/redisson-config/single-server.yaml").getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Redisson.create(config);
    }

    @Bean("redissonMasterClient")
    public RedissonClient redissonMasterClient(){
//        Config config = null;
//        try {
//            config = Config.fromYAML(new ClassPathResource("/redisson-config/master-salve-server.yaml").getInputStream());
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return Redisson.create(config);
        return Redisson.create();
    }

    @Bean("redissonSentinelClient")
    public RedissonClient redissonSentinelClient(){
//        Config config = null;
//        try {
//            config = Config.fromYAML(new ClassPathResource("/redisson-config/sentinel.yaml").getInputStream());
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return Redisson.create(config);
        return Redisson.create();
    }
}
